/*
 * Copyright (C) 2018 wxyz <hyhjwzx@126.com>
 * This program can be distributed under the terms of the GNU GPL Version 2.
 * See the file LICENSE.
 *
 */

package ren.wxyz.study.euler.hp01;

import ren.wxyz.study.euler.base.IProblem;
import ren.wxyz.study.euler.util.ConsoleHelper;
import ren.wxyz.study.euler.util.PathHelper;

import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

/**
 * 第 8 题
 *
 * @auther wxyz 2018-03-08_20:17
 * @since 1.0
 */
public class P008 implements IProblem {
    @Override
    public void main(String[] args) throws Throwable {
        int limitNumber = Integer.parseInt(args[0]);
        List<String> numStr = Files.readAllLines(Paths.get(PathHelper.getClassPath(P008.class),"hp01/P008/input.txt"), Charset.defaultCharset());
        int[] nums = new int[1000];

        // 读取参数
        for (int i = 0; i < numStr.size(); i++) {
            String lineNum = numStr.get(i);
            for (int j = 0; j < 50; j++) {
                nums[i * 50 + j] = lineNum.charAt(j) - '0';
            }
        }

        // 查找
        long maxNum = 0;
        int startIndex = 0;
        for (int i = 0; i <= 1000 - limitNumber; i++) {
            long product = 1;
            for (int j = 0; j < limitNumber; j++) {
                product *= nums[i + j];
            }

            if (product > maxNum) {
                maxNum = product;
                startIndex = i;
            }
        }

        ConsoleHelper.printf("连续 %1$d 个数字的最大乘积是 %2$d", limitNumber, maxNum);
        ConsoleHelper.print();
        ConsoleHelper.print("分别是 "  + Arrays.toString(Arrays.copyOfRange(nums, startIndex, startIndex + limitNumber)));
    }
}
